package com.st.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * <p>title: com.wfx.filters</p>
 * author zhuximing
 * description:
 */
@Component
public class CalServiceTimeGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {


    @Override
    public GatewayFilter apply(NameValueConfig config) {


        System.out.println(config.getName());


        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

                //过滤器的前置处理
                System.out.println("pre come in");
                //获取系统的当前时间为startTime
                long start = System.currentTimeMillis();


//                return chain.filter(exchange);//放行
                return  chain.filter(exchange).then( //post逻辑
                    Mono.fromRunnable(()->{
                        System.out.println("post come in");
                        //获取系统当前时间戳为endTime
                        long entTime = System.currentTimeMillis();
                        System.out.println("time="+(entTime-start));

                    }));
            }
        };
    }

    @Override
    public ShortcutType shortcutType() {
        return ShortcutType.DEFAULT;
    }
}
